﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Drawing.Imaging;

using LowLevelGraphics.Histograms;

//TODO: Homogenity: Test

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Homogenity
    /// </summary>
    public class Homogenity : BaseForeBackgroundColorFilter
    {
        protected int m_nThreshold = 7;
        protected int m_nWindowSize = 3;

        /// <summary>
        /// Initializes a new instance of the <see cref="EnergyMap"/> class.
        /// </summary>
        public Homogenity()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EnergyMap"/> class.
        /// </summary>
        /// <param name="_homogenity">The _homogenity.</param>
        internal Homogenity(Homogenity _homogenity)
        {
        }

        /// <summary>
        /// Execute this Filter
        /// A Debug Statement is written if a concrete Filter is called
        /// </summary>
        /// <param name="_bitmap"></param>
        /// <returns></returns>
        public override UnsafeBitmap Execute(UnsafeBitmap _bitmap)
        {
            UnsafeBitmap bitmap = _bitmap;
            //            histogram.SortBy(PROCESSING_MODE.GrayColor);

            int nWidth = bitmap.Width;
            int nHeight = bitmap.Height;

            int y = 0;
            int x = 0;
            int kx = 0;
            int ky = 0;

            Color color = Color.Transparent;
            int nGray = 0;
            int nGraySum = 0;

            int[] aValue = new int[256];

            int nGrayTemp = 0;

            int nValue = 0;
            int nGrayValue = 0;
            int nNewGrayValue = 0;
            int nDiff = 0;

            int nSum = 0;
            for (y = m_nWindowSize; y < nHeight-m_nWindowSize; y++)
            {
                for (x = 0; x < nWidth - m_nWindowSize; x++)
                {
                    nGrayValue = _bitmap.GetGrayPixelValue(x, y);
                    nSum = 0;
                    for (ky = 0; ky < m_nWindowSize; ky++)
                    {
                        for (kx = 0; kx < m_nWindowSize; kx++)
                        {
                            nValue = _bitmap.GetGrayPixelValue(x + kx, y + ky);
                            nSum += Math.Abs(nValue - nGrayValue);
                        }
                    }
                    nDiff = Math.Abs(nSum - nGrayValue);
                    if (nDiff == 0) nDiff = 1;
                    nNewGrayValue = 255 / nDiff;
                    if (nNewGrayValue == 0) nNewGrayValue = 1;
                    _bitmap.SetPixel(x,y,nSum<m_nThreshold ? m_ColorBackground : Color.FromArgb(nNewGrayValue, nNewGrayValue, nNewGrayValue));
                }                
            }
            return _bitmap;
        }

        /// <summary>
        /// This is used to clone a filter, each filter must implement it's own clone functionality
        /// </summary>
        /// <returns></returns>
        public override object Clone()
        {
            return new Homogenity(this);
        }
    }
}
